var $$ = Dom7;
var GlobalUserid,GlobalOpenid,GlobalFirst;
var Sysmsgnum=0;
var Usermsgnum=0;
// var Globaltoken;
// var Commappid = '1asffnae6ue8';
var ApiUrl = 'http://api.nldou.com/';
var initjssdk_url = ApiUrl+'weixinapi/init_wx_jsapi';
var Prepay_url = ApiUrl+'wxpay/prepay';
// var Nativepay_url = ApiUrl+'wxpay/nativepay';
var Share_url = ApiUrl+'ucenter/weixinlogin?pid=7653';
var Sharecallback_url = ApiUrl+'commwapucenter/sharecallback';
var Shareimg_url = 'http://wap.nldou.com/comm/img/comm-logo.jpg';

var Qrank_url = ApiUrl+'commwapbeans/getQuestionrank';
var Qresponsed_url = ApiUrl+'commwapbeans/getQResponsed';
var GetNotifyCount_url = ApiUrl+'commwapnotify/getNotifyUnread';
var GetUserNotify_url = ApiUrl+'commwapnotify/getUserNotify';
var GetSysNotify_url = ApiUrl+'commwapnotify/getSysnotify';
var ReadNotify_url = ApiUrl+'commwapnotify/readNotify';
var ClearNotify_url = ApiUrl+'commwapnotify/clearNotify';
//discover controller
var CheckUserLogin_url = ApiUrl+'ucenter/checkUserLogin';
var CheckUserRedirect_url = ApiUrl+'ucenter/weixinlogin?pid=7653';
var GetDiscoverData_url = ApiUrl+'commwapdiscover/';
var GetDiscoverQuestion_url = ApiUrl+'commwapdiscover/getQuestion';
//question controller
var AddQuestionViews_url = ApiUrl+'commwapquestion/readQuestion';
var GetQuestionQdetail_url = ApiUrl+'commwapquestion/';
var GetQuestionAitem_url = ApiUrl+'commwapquestion/getAnswer';
var FollowQuestion_url = ApiUrl+'commwapquestion/followQuestion';
var CancelFollowQuestion_url = ApiUrl+'commwapquestion/cancelfollowQuestion';
var GetQuestionSubject_url = ApiUrl+'commwapquestion/getSubject';
var AddQuestion_url = ApiUrl+'commwapquestion/addQuestion';
var Beansrank_url = ApiUrl+'commwapbeans/getBeansrankByQid';
var Dechargebeans_url = ApiUrl+'commwapbeans/dechargebeans';
var Getcontributer_url = ApiUrl+'commwapquestion/getQContributer';
//answer controller
var AddAnswer_url = ApiUrl+'commwapanswer/addAnswer';
var UpdateAnswer_url = ApiUrl+'commwapanswer/updateAnswer';
var CheckAnswerExist_url = ApiUrl+'commwapanswer/checkAnswerExist';
var GetAnwserAdetail_url = ApiUrl+'commwapanswer/';
var AgreeAnswer_url = ApiUrl+'commwapanswer/agreeAnswer';
var CancelAgreeAnswer_url = ApiUrl+'commwapanswer/cancelagreeAnswer';
var CollectAnswer_url = ApiUrl+'commwapanswer/collectAnswer';
var CancelCollectAnswer = ApiUrl+'commwapanswer/cancelcollectAnswer';
var GetAnswerCitem_url = ApiUrl+'commwapanswer/getComment';
var AddComment_url = ApiUrl+'commwapanswer/addComment';
//comment controller
var thumbupComment_url = ApiUrl+'commwapcomment/thumbup';
var cancelThumbup_url = ApiUrl+'commwapcomment/cancelThumbup';
var AddResponse_url = ApiUrl+'commwapcomment/responseComm';
var GetCommentsResponse_url = ApiUrl+'commwapcomment/getResponse';
//subject controller
var GetSubByid_url = ApiUrl+'commwapsubject/getSubById';
var FollowSub_url = ApiUrl+'commwapsubject/followSub';
var CancelFollowSub_url = ApiUrl+'commwapsubject/cancelfollowSub';
var GetQBySid_url = ApiUrl+'commwapsubject/getQuestionBySid';
//ucenter controller
var GetUcenterInfo_url = ApiUrl+'commwapucenter/getInfo';
var Sign_url = ApiUrl+'commwapucenter/sign';
var GetMyfollowq_url = ApiUrl+'commwapucenter/getFollowq';
var GetMyfollowsub_url = ApiUrl+'commwapucenter/getFollowsub';
var GetMycollect_url = ApiUrl+'commwapucenter/getCollects';
var GetMyanswer_url = ApiUrl+'commwapucenter/getUserAnswer';
var GetMyquestion_url = ApiUrl+'commwapucenter/getUserQuestion';
var Chargeuser_url = ApiUrl+'commwapucenter/getUserInfo';
var Uploadimg_url = ApiUrl+'commwapucenter/changeIcon';
var Getprofile_url = ApiUrl+'commwapucenter/getProfile';
var Getcity_url = ApiUrl+'gamer/getCityList';
var Getarea_url = ApiUrl+'gamer/getAreaListBycity';
var Subprofile_url = ApiUrl+'commwapucenter/updateProfile';

var discoverpagesize = 5;/*发现页问题列表*/
var questionpagesize = 5;/*问题页答案列表*/
var contripagesize = 20;/*问题页贡献者展示最大个数*/
var rankpagesize = 10;/*贡献榜列表*/
var answerpagesize = 5;/*答案页评论列表*/
var responsepagesize = 20;/*答案页评论列表*/
var subjectpagesize = 5;/*话题页问题列表*/
var myfollowspagesize = 10;/*个人中心我的关注列表*/
var mycollectspagesize = 5;/*个人中心我的收藏列表*/
var myanswerpagesize = 5;/*个人中心我的回答列表*/
var myquestionpagesize = 5;/*个人中心我的提问列表*/
var notifypagesize = 10;/*消息列表*/

var discoverloading = false;/*发现页问题列表下拉刷新状态*/
var questionloading = false;/*问题页答案列表下拉刷新状态*/
var rankloading = false;/*贡献榜下拉刷新状态*/
var qrankloading = false;/*女神榜下拉刷新状态*/
var answerloading = false;/*答案页评论列表下拉刷新状态*/
var responseloading = false;/*答案页评论回复列表下拉刷新状态*/
var subjectloading = false;/*话题页评问题列表下拉刷新状态*/
var myfollowsloading = false;/*个人中心我的关注列表下拉刷新状态*/
var mycollectsloading = false;/*个人中心我的收藏列表下拉刷新状态*/
var myanswerloading = false;/*个人中心我的回答列表下拉刷新状态*/
var myquestionloading = false;/*个人中心我的提问列表下拉刷新状态*/
var notifyloading = false;/*消息列表下拉刷新状态*/

var answeruploading = false;/*答案发布状态*/
var commentuploading = false;/*评论发布状态*/
var responseuploading = false;/*回复发布状态*/
var questionuploading = false;/*问题发布状态*/
var fsubstatus = false;
var fqstatus = false;
var collstatus = false;
var agreestatus = false;
var thumbupstatus = false;
var profileuploading = false;
var chargestatus = false;
var dechargestatus = false;

/******判断登陆状态******/
checkuserlogin(CheckUserLogin_url,CheckUserRedirect_url);
// GlobalUserid = 3;

/****微信js sdk初始化****/
$$.ajax({
    url:initjssdk_url,
    method:"GET",
    data: {fromUrl:window.location.href},
    dataType:'json',
    success: function (res) {
        //console.log('初始化jssdk',res);
        if(res.code==1){
            wx.config({
                debug: false,
                appId: res.data.appid,
                timestamp: res.data.timestamp,
                nonceStr: res.data.nonceStr,
                signature: res.data.signature,
                jsApiList: ['chooseWXPay','onMenuShareTimeline','onMenuShareAppMessage','hideMenuItems','chooseImage'
                    ,'uploadImage','getLocalImgData']
            });
        }
    }
});
wx.ready(function () {
    wx.hideMenuItems({
        menuList: ['menuItem:share:qq','menuItem:share:weiboApp','menuItem:share:facebook','menuItem:share:QZone',
            'menuItem:copyUrl','menuItem:openWithQQBrowser','menuItem:openWithSafari']
    });
    wx.onMenuShareTimeline({
        title: '一问奕答',
        link: Share_url,
        imgUrl: Shareimg_url,
        success: function () {
            sharecallback(Share_url,'timeline');
            return true;
        }
    });
    wx.onMenuShareAppMessage({
        title: '一问奕答',
        desc: '复旦大学社会学教授沈奕斐，为您解答家庭教育的各类问题，陪您共走育儿路',
        link: Share_url,
        imgUrl: Shareimg_url,
        type: 'link',
        success: function () {
            sharecallback(Share_url,'appmsg');
            return true;
        }
    });
    $$(document).on('click','.charge-item',function(){
        if (chargestatus) return;
        chargestatus=true;
        var id = parseInt($$(this).data('id'));
        var num = 0;
        switch (id){
            case 1:
                num = 50;
                break;
            case 2:
                num = 100;
                break;
            case 3:
                num = 500;
                break;
            case 4:
                num = 1000;
                break;
            case 5:
                num = 5000;
                break;
            case 6:
                num = 10000;
                break;
            case 7:
                num = 20000;
                break;
            case 8:
                num = 50000;
                break;
            default:
                return false;
        }
        pay(num);
    });
    // $$(document).on('click','.othercharge-item',function(){
    //     Nldouapp.prompt('其他数目','',function(val){
    //         var num = checkcharge(val);
    //         pay(num);
    //     });
    // });
});
wx.error(function (res) {
});

/****Initialize your app****/
var Nldouapp = new Framework7({
    animateNavBackIcon: true,
    modalTitle:'一问奕答',
    modalButtonOk:'确认',
    modalButtonCancel:'取消',
    template7Pages:true,
    swipeBackPage:false,
    pushState:true,
    pushStateSeparator:'#page/'
});

/****Add view****/
var mainView = Nldouapp.addView('.view-main', {
    dynamicNavbar: true
});
/****预编译模板****/
Nldouapp.SwiperslideTemplate = Template7.compile($$('script#Swiperslide').html());
Nldouapp.SwiperslideRectTemplate = Template7.compile($$('script#SwiperslideRect').html());
Nldouapp.NotifylistTemplate = Template7.compile($$('script#Notifylist').html());
Nldouapp.NotifyiconTemplate = Template7.compile($$('script#Notifyicon').html());
Nldouapp.Notifyicon2Template = Template7.compile($$('script#Notifyicon2').html());
Nldouapp.SofaTemplate = Template7.compile($$('script#Sofa').html());
Nldouapp.EmptyTemplate = Template7.compile($$('script#Empty').html());
Nldouapp.QitemTemplate = Template7.compile($$('script#Qitem').html());
Nldouapp.RanklistTemplate = Template7.compile($$('script#Ranklist').html());
Nldouapp.QranklistTemplate = Template7.compile($$('script#Qranklist').html());
Nldouapp.QreslistTemplate = Template7.compile($$('script#Qreslist').html());
Nldouapp.QdetailTemplate = Template7.compile($$('script#Qdetail').html());
Nldouapp.QdetailwrapTemplate = Template7.compile($$('script#Qdetailwrap').html());
Nldouapp.QContributerTemplate = Template7.compile($$('script#Contributer').html());
Nldouapp.AnsweritemTemplate = Template7.compile($$('script#Aitem').html());
Nldouapp.EditorTemplate = Template7.compile($$('script#Editor').html());
Nldouapp.QeditorTemplate = Template7.compile($$('script#Qeditor').html());
Nldouapp.AdetailnavTemplate = Template7.compile($$('script#Adetailnav').html());
Nldouapp.AdetailtoolbarTemplate= Template7.compile($$('script#Adetailtoolbar').html());
Nldouapp.AdetailcontentTemplate = Template7.compile($$('script#Adetailcontent').html());
Nldouapp.CommentitemTemplate = Template7.compile($$('script#Citem').html());
Nldouapp.ResponseitemTemplate = Template7.compile($$('script#Ritem').html());
Nldouapp.ResponsnavTemplate = Template7.compile($$('script#Responsenav').html());
Nldouapp.SubdetailTemplate = Template7.compile($$('script#Subdetail').html());
Nldouapp.UcHeaderTemplate = Template7.compile($$('script#Ucheader').html());
Nldouapp.SubitemTemplate = Template7.compile($$('script#Sitem').html());
Nldouapp.CollectitemTemplate = Template7.compile($$('script#Collectitem').html());
Nldouapp.MyansweritemTemplate = Template7.compile($$('script#Myansweritem').html());
Nldouapp.MyquestionitemTemplate = Template7.compile($$('script#Myquestionitem').html());
Nldouapp.ChargeuserTemplate = Template7.compile($$('script#Chargeuser').html());
Nldouapp.ProfilelistTemplate = Template7.compile($$('script#Profilelist').html());
Nldouapp.CitylistTemplate = Template7.compile($$('script#Citylist').html());
Nldouapp.ArealistTemplate = Template7.compile($$('script#Arealist').html());
Nldouapp.ChilditemTemplate = Template7.compile($$('script#Childitem').html());

showNfirst(GlobalFirst);

/******事件******/
$$(document).on('click','.toggle-text-icon',function () {
    if ($$(this).hasClass('active')){
        $$(this).removeClass('active');
        $$(this).find('i').html('chevron_down');
        $$(this).parent().find('.toggle-text').css('max-height','100px');
    }else{
        $$(this).addClass('active');
        $$(this).find('i').html('chevron_up');
        $$(this).parent().find('.toggle-text').css('max-height','10000px');
    }
});
$$(document).on('click','.dicvbtn',function () {
    if ($$('.view-main').data('page')==='Discover') mainView.router.refreshPage();
    else mainView.router.load({url:'index.html'});
});
//刷新发现页热门问题
$$(document).on('click','.refdiscbtn',function(){
    if (discoverloading) return;
    discoverloading = true;
    getQuestion(1,GetDiscoverQuestion_url,false);
});
//进入消息页面
$$(document).on('click','.notifybtn',function(){
    mainView.router.load({url:'notify.html'});
});
//消息
$$(document).on('click','.unotifylistbtn',function () {
    $$('.nlistbtn.active').removeClass('active');
    $$(this).addClass('active');
    $$('#Notify .notifylistwrap').data('type','user');
    getUserNotify(GetUserNotify_url,1,false);
});
//系统通知
$$(document).on('click','.sysnotifylistbtn',function () {
    $$('.nlistbtn.active').removeClass('active');
    $$(this).addClass('active');
    $$('#Notify .notifylistwrap').data('type','sys');
    getSysNotify(GetSysNotify_url,1,false);
});
//阅读消息
$$(document).on('click','.nitem',function () {
    var type = $$(this).data('msgtype');
    var id = $$(this).data('msgid');
    readNotify(ReadNotify_url,id,type);
});
//删除消息
$$(document).on('click','.nitem-delete',function () {
    var type = $$(this).data('msgtype');
    var id = $$(this).data('msgid');
    readNotify(ReadNotify_url,id,type);
});
//清空消息
$$(document).on('click','.clearnotifybtn',function () {
    Nldouapp.confirm('确定清空所有消息？','',function(){
        clearNotify(ClearNotify_url);
    });
});
//进入问题页面
$$(document).on('click','.qitem',function () {
    var qid = $$(this).data('qid');
    //增加阅读量
    // readQuestion(AddQuestionViews_url,qid);
    mainView.router.load({url:'question.html?qid='+qid});
});
//进入问题贡献榜
$$(document).on('click','.conitem',function () {
    var qid = $$("#Question").data('id');
    mainView.router.load({url:'beansrank.html?qid='+qid});
});
//进入女神榜
$$(document).on('click','.qrankbtn',function () {
    mainView.router.load({url:'questionrank.html'});
});
//女神榜未回答
$$(document).on('click','.qranklistbtn',function () {
    $$('.qlistbtn.active').removeClass('active');
    $$(this).addClass('active');
    $$('#Questionrank .qlistwrap').data('type','rank');
    getQrank(Qrank_url,1);
});
//女神榜已回答
$$(document).on('click','.qreslistbtn',function () {
    $$('.qlistbtn.active').removeClass('active');
    $$(this).addClass('active');
    $$('#Questionrank .qlistwrap').data('type','res');
    getQresponsed(Qresponsed_url,1,false);
});
//进入话题页面
$$(document).on('click','.subitem',function () {
    var sid = $$(this).data('sid');
    mainView.router.load({url:'subject.html?sid='+sid});
});
//进入个人中心
$$(document).on('click','.ucbtn',function(){
    mainView.router.load({url:'ucenter.html'});
});
//上传头像
$$(document).on('click','.editiconbtn',function (){
    wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
            changeicon(res.localIds[0]);
        }
    });
});
//个人中心我的关注
$$(document).on('click','.myfollows',function () {
    mainView.router.load({url:'myFollows.html'});
});
//个人中心我的关注->问题
$$(document).on('click','.myfollowqbtn',function () {
    $$('.followbtn.active').removeClass('active');
    $$(this).addClass('active');
    $$('#Myfollows .followlist').data('type','question');
    getMyfollowq(GetMyfollowq_url,1,false);
});
//个人中心我的关注->话题
$$(document).on('click','.myfollowsubbtn',function () {
    $$('.followbtn.active').removeClass('active');
    $$(this).addClass('active');
    $$('#Myfollows .followlist').data('type','subject');
    getMyfollowsub(GetMyfollowsub_url,1,false);
});
//个人中心我的收藏
$$(document).on('click','.myCollects',function () {
    mainView.router.load({url:'myCollects.html'});
});
//个人中心我的回答
$$(document).on('click','.myAnswers',function () {
    mainView.router.load({url:'myAnswers.html'});
});
//个人中心我的提问
$$(document).on('click','.myQuestions',function () {
    mainView.router.load({url:'myQuestions.html'});
});
//个人中心签到
$$(document).on('click','.signbtn',function () {
    signToday(Sign_url);
});
//个人中心充值
$$(document).on('click','.beansbtn',function () {
    mainView.router.load({url:'chargebeans.html'});
});
//编辑个人信息
$$(document).on('click','.editprofile',function () {
    mainView.router.load({url:'profile.html'});
});
//添加孩子信息
$$(document).on('click','.addchildbtn',function () {
    mainView.router.load({url:'childinfo.html'});
});
var childdelte = [];
//删除孩子信息
$$(document).on('click','.childitem',function () {
    Nldouapp.swipeoutOpen($$(this), 'right');
});
$$(document).on('click','.childitem-delete',function () {
    var id = parseInt($$(this).data('id'));
    childdelte.push(id);
});
//提交个人信息
$$(document).on('click','.subprofile',function () {
    if (profileuploading) return;
    profileuploading = true;
    var data = checkProfile();
    if (data) subprofile(data);
});
//如何赚取能量豆
$$(document).on('click','.beansguide',function () {
    mainView.router.load({url:'beansguide.html'});
});
//关注话题
$$(document).on('click','.subfollowbtn',function () {
    if (fsubstatus) return;
    fsubstatus = true;
    var sid = $$('#Subject').data('id');
    followSub(FollowSub_url,sid);
    var parent = $$(this).parent();
    $$(this).remove();
    var html = '<span class="bg-gray border-r5 color-white subfollowcancelbtn size-14" style="line-height: 30px;width: 70px;height: 30px;text-align: center;">已关注</span>';
    parent.append(html);
});
//话题取消关注
$$(document).on('click','.subfollowcancelbtn',function () {
    if (fsubstatus) return;
    fsubstatus = true;
    var sid = $$('#Subject').data('id');
    cancelfollowSub(CancelFollowSub_url,sid);
    var parent = $$(this).parent();
    $$(this).remove();
    var html = '<div class="color-white bg-green flex-row flex-nowrap flex-justify-around flex-align-center border-r5 subfollowbtn" style="width: 50px;height: 30px;padding:0 10px;">'+
        '<i class="f7-icons color-white" style="height: 100%;">add</i>'+
        '<span style="line-height: 30px;" class="size-14">关注</span>'+
        '</div>';
    parent.append(html);
});
//话题问题排序模式
$$(document).on('click',".subsortinput",function () {
    var label = $$(this).find('.item-title').html();
    var sort = $$(this).find('input').val();
    $$('#Subject .subsortbtn').html(label);
    $$('#Subject .infinite-scroll').data('sort',sort);

    var sid = $$('#Subject').data('id');
    getQuestionBysid(GetQBySid_url,1,sid,sort,false);

    Nldouapp.closeModal('.popover-subsort');
});
//用户提问
$$(document).on('click','.editqbtn',function(){
    pubQuestion(GetQuestionSubject_url);
});
//用户取消提问
$$(document).on('click','.closeqeditbtn',function(){
    Nldouapp.closeModal('.popup-qedit');
    $$('.popup-qedit').remove();
});
//用户发表提问
$$(document).on('click','.qeditpub',function(){
    var title = $$('input[name="qtitle"]').val();
    var subject = parseInt($$('select[name="qsub"]').val());
    var desc = $$('textarea[name="qdesc"]').val();
    var res = checkQuestion(title,subject,desc);
    if (res) addQuestion(AddQuestion_url,res.subject,res.title,res.desc);
});
//问题答案排序模式
$$(document).on('click',".asortinput",function () {
    var label = $$(this).find('.item-title').html();
    var sort = $$(this).find('input').val();
    $$('#Question .asortbtn').html(label);
    $$('#Question .infinite-scroll').data('sort',sort);

    var qid = $$('#Question').data('id');
    getAnswerByQid(qid,1,GetQuestionAitem_url,sort,false);

    Nldouapp.closeModal('.popover-answersort');
});
//关注问题
$$(document).on('click','.qfollowbtn',function () {
    if (fqstatus) return;
    fqstatus = true;
    var qid = $$('#Question').data('id');
    followQuestiion(FollowQuestion_url,qid);
    var parent = $$(this).parent();
    $$(this).remove();
    var html = '<span class="bg-gray border-r5 color-white qfollowcancelbtn" style="line-height: 30px;width: 70px;height: 30px;text-align: center;">已关注</span>';
    parent.append(html);

});
//问题取消关注
$$(document).on('click','.qfollowcancelbtn',function () {
    if (fqstatus) return;
    fqstatus = true;
    var qid = $$('#Question').data('id');
    cancelfollowQuestiion(CancelFollowQuestion_url,qid);
    var parent = $$(this).parent();
    $$(this).remove();
    var html = '<div class="color-white bg-green flex-row flex-nowrap flex-justify-around flex-align-center border-r5 qfollowbtn" style="width: 50px;height: 30px;padding:0 10px;">'+
        '<i class="f7-icons color-white" style="height: 100%;">add</i>'+
        '<span class="" style="line-height: 30px;">关注</span>'+
        '</div>';
    parent.append(html);
});
//贡献能量豆
$$(document).on('click','.givebeans-btn',function () {
    mainView.router.load({url:'dechargebeans.html'});
});
$$(document).on('click','.decharge-item',function(){
    if (dechargestatus) return;
    dechargestatus = true;
    var id  = parseInt($$(this).data('id'));
    var qid = $$('#Question').data('id');
    var num = 0;
    switch (id){
        case 1:
            num = 50;
            break;
        case 2:
            num = 100;
            break;
        case 3:
            num = 500;
            break;
        case 4:
            num = 1000;
            break;
        case 5:
            num = 5000;
            break;
        case 6:
            num = 10000;
            break;
        default:
            return false;
    }
    dechargebeans(Dechargebeans_url,num,qid);
});
$$(document).on('click','.otherdecharge-item',function(){
    var qid = $$('#Question').data('id');
    Nldouapp.prompt('其他数目','',function(val){
        dechargebeans(Dechargebeans_url,val,qid);
    });
});
//用户发布答案
$$(document).on('click','.editanswerbtn',function(){
    var id = $$('#Question').data('id');
    //判断用户是否已经回答过
    //如果回答过，修改答案
    checkAnswerExist(CheckAnswerExist_url,id,GlobalUserid);
});
//用户发布评论
$$(document).on('click','.commentbtn',function () {
    var aid = $$(this).data('aid');
    var html = Nldouapp.EditorTemplate({'id':aid,'cid':'','type':'addComment','title':'添加评论','suggest':'填写评论内容','content':''});
    $$('body').append(html);
    Nldouapp.popup('.popup-edit');

});
//用户发布回复
$$(document).on("click",".responsebtn",function () {
    var cid = $$(this).data('cid');
    var html = Nldouapp.EditorTemplate({'id':cid,'cid':'','type':'addResponse','title':'添加回复','suggest':'填写回复内容','content':''});
    $$('body').append(html);
    Nldouapp.popup('.popup-edit');
});
//用户取消发布
$$(document).on('click','.closeeditbtn',function(){
    Nldouapp.closeModal('.popup-edit');
    $$('.popup-edit').remove();
});
//用户发布
$$(document).on('click','.editpub',function(){
    var id = $$(this).data('id');
    var cid = $$(this).data('cid');
    var type = $$(this).data('type');
    var content = $$('textarea[name="editcontent"]').val();
    switch (type){
        case 'updateAnswer':
            //校验用户输入内容
            content = checkAnswer(content);
            if (!content) return;
            updateAnswer(UpdateAnswer_url,cid,content);
            break;
        case 'addAnswer':
            content = checkAnswer(content);
            if (!content) return;
            addAnswer(AddAnswer_url,id,GlobalUserid,content);
            break;
        case 'addComment':
            content = checkComment(content);
            if (!content) return;
            addComment(AddComment_url,id,GlobalUserid,content);
            break;
        case 'addResponse':
            content = checkResponse(content);
            if (!content) return;
            responseComment(AddResponse_url,id,GlobalUserid,content);
            break;
        default:
    }
});
//用户点击答案
$$(document).on('click','.aitem',function () {
    var aid = $$(this).data('aid');
    mainView.router.load({url:'answer.html?aid='+aid});
});
//用户点击赞同
$$(document).on('click','.agreebtn',function () {
    if (agreestatus) return;
    agreestatus = true;
    var aid = $$(this).data('aid');
    agreeAnswer(aid,AgreeAnswer_url);
    var agreenum = parseInt($$(this).data('agrees'))+1;
    var html = '<div class="cancelagreebtn active" data-aid="'+aid+'" data-agrees="'+agreenum+'">'+
        '<div class="toolbar-icon-item flex-column flex-justify-between flex-align-center flex-wrap">'+
        '<i class="toolbar-icon f7-icons">heart_fill</i>'+
        '<span class="toolbar-icon-text">已赞同('+agreenum+')</span>'+
        '</div></div>';
    $$(this).parent().html(html);
});
//用户取消赞同
$$(document).on('click','.cancelagreebtn',function () {
    if (agreestatus) return;
    agreestatus = true;
    var aid = $$(this).data('aid');
    cancelagreeAnswer(aid,CancelAgreeAnswer_url);
    var agreenum = parseInt($$(this).data('agrees'))-1;
    var html = '<div class="agreebtn" data-aid="'+aid+'" data-agrees="'+agreenum+'">'+
        '<div class="toolbar-icon-item flex-column flex-justify-between flex-align-center flex-wrap">'+
        '<i class="toolbar-icon f7-icons">heart_fill</i>'+
        '<span class="toolbar-icon-text">赞同('+agreenum+')</span>'+
        '</div></div>';
    $$(this).parent().html(html);
});
//用户点击收藏
$$(document).on('click','.collectbtn',function () {
    if (collstatus) return;
    collstatus = true;
    var aid = $$(this).data('aid');
    collectAnswer(aid,CollectAnswer_url);
    var collectnum = parseInt($$(this).data('collects'))+1;
    var html = '<div class="cancelcollectbtn active" data-aid="'+aid+'" data-collects="'+collectnum+'">'+
        '<div class="toolbar-icon-item flex-column flex-justify-between flex-align-center flex-wrap">'+
        '<i class="toolbar-icon f7-icons">star_fill</i>'+
        '<span class="toolbar-icon-text">已收藏('+collectnum+') </span>'+
        '</div></div>';
    $$(this).parent().html(html);
});
//用户取消收藏
$$(document).on('click','.cancelcollectbtn',function () {
    if (collstatus) return;
    collstatus = true;
    var aid = $$(this).data('aid');
    cancelcollectAnswer(aid,CancelCollectAnswer);
    var collectnum = parseInt($$(this).data('collects'))-1;
    var html = '<div class="collectbtn" data-aid="'+aid+'" data-collects="'+collectnum+'">'+
        '<div class="toolbar-icon-item flex-column flex-justify-between flex-align-center flex-wrap">'+
        '<i class="toolbar-icon f7-icons">star_fill</i>'+
        '<span class="toolbar-icon-text">加入收藏('+collectnum+') </span>'+
        '</div></div>';
    $$(this).parent().html(html);
});
//答案评论排序模式
$$(document).on('click',".csortinput",function () {
    var label = $$(this).find('.item-title').html();
    var sort = $$(this).find('input').val();
    $$('#Answer .csortbtn').html(label);
    $$('#Answer .infinite-scroll').data('sort',sort);

    var aid = $$('#Answer').data('id');
    getCommentsByAid(aid,1,GetAnswerCitem_url,sort,false);

    Nldouapp.closeModal('.popover-commentsort');
});
//用户评论点赞
$$(document).on("click",".thumbupbtn",function () {
    if (thumbupstatus) return;
    thumbupstatus = true;
    var cid = $$(this).data('cid');
    thumbupComment(cid,thumbupComment_url);
    var thumbnum = parseInt($$(this).data('thumbup'))+1;
    var html = '<div class="cancelthumbupbtn flex-row flex-nowrap flex-justify-between flex-align-center" data-cid="'+cid+'" data-thumbup="'+thumbnum+'" ' +
        'style="width: 25px;">'+
        '<i class="f7-icons size-12">heart_fill</i>'+
        '<span>'+thumbnum+'</span>'+
        '</div>';
    $$(this).parent().html(html);
});
//用户评论点赞
$$(document).on("click",".cancelthumbupbtn",function () {
    if (thumbupstatus) return;
    thumbupstatus = true;
    var cid = $$(this).data('cid');
    cancelthumbupComment(cid,cancelThumbup_url);
    var thumbnum = parseInt($$(this).data('thumbup'))-1;
    var html = '<div class="thumbupbtn flex-row flex-nowrap flex-justify-between flex-align-center" data-cid="'+cid+'" data-thumbup="'+thumbnum+'" ' +
        'style="width: 30px;">'+
        '<i class="f7-icons size-12">heart</i>'+
        '<span>'+thumbnum+'</span>'+
        '</div>';
    $$(this).parent().html(html);
});
//用户查看回复
$$(document).on("click",".showresponse",function () {
    var cid = $$(this).data('cid');
    var aid = $$('#Answer').data('id');
    mainView.router.load({url:'response.html?cid='+cid+'&aid='+aid});
});